<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/24
 * Time: 18:56
 */

namespace App\Service;

use EasySwoole\MysqliPool\Mysql;

class MatchService
{
    /**
     * lol比赛列表
     * @param $params
     * @return array
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \EasySwoole\Mysqli\Exceptions\WhereParserFail
     * @throws \Throwable
     */
    public $bet = [
        [
            'source'    => 'pinnacle',
            'name'    => '平博',
            'team_a'    => [],
            'team_b'    => []
        ],
        [
            'source'    => '雷竞技',
            'name'    => '雷竞技',
            'team_a'    => [],
            'team_b'    => []
        ],
        [
            'source'    => 'bet365',
            'name'    => 'bet365',
            'team_a'    => [],
            'team_b'    => []
        ],
        [
            'source'    => 'anggame',
            'name'    => '安博',
            'team_a'    => [],
            'team_b'    => []
        ]
    ];

    /**
     * lol比赛列表
     * @param $params
     * @return array
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \EasySwoole\Mysqli\Exceptions\WhereParserFail
     * @throws \Throwable
     */
    public function lolMatchLists($params)
    {
        $db = Mysql::defer('mysql');
        $status = isset($params['status']) ? [$params['status']] : [0,1,2];
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $sort =  $params['sort'] ?? 'asc';

        if( isset($params['start_date']) && isset($params['end_date']) ){
            $start_time = strtotime($params['start_date'])*1000;
            $end_time = strtotime($params['end_date'])*1000;
            $db->where('start_time',$start_time,'>=')->where('start_time',$end_time,'<');
        }

        $lists = $db->whereIn('status',$status)
            ->orderBy('start_time',$sort)
            ->get('siam_lolmatchs',[$pageSize * ($page  - 1), $pageSize]); //比赛列表
        foreach ($lists as $k=>$v){
            $lists[$k]['start_time'] = $this->getMsecToMescdate($v['start_time']);
            $lists[$k]['match_time'] = $v['start_time'];
            $lists[$k]['battle_ids'] = json_decode($v['battle_ids'],true);
            $team_b = $db->where('team_id',$v['team_b_id'])->getOne('siam_lolteams','name,name_en,short_name,logo') ? : [];//队伍a详情
            $team_a = $db->where('team_id',$v['team_a_id'])->getOne('siam_lolteams','name,name_en,short_name,logo') ? : [];;//队伍b详情
            $bets = $db->where('match_id',$v['match_id'])->where('bet_type',1)->get('siam_lolbet',null,'options,source');
            $lists[$k]['team_a'] = $team_a;
            $lists[$k]['team_b'] = $team_b;

            //赔率
            if( $bets ){
                foreach ( $bets as $k2=>$v2 ){
                    $options = json_decode($v2['options'],true);
                    if( $options[0]['team_id'] == $v['team_b_id'] ){
                        $bets[$k2]['team_b'] = $options[0];
                    }else{
                        $bets[$k2]['team_a'] = $options[0];
                    }
                    if( $options[1]['team_id'] == $v['team_b_id'] ){
                        $bets[$k2]['team_b'] = $options[1];
                    }else{
                        $bets[$k2]['team_a'] = $options[1];
                    }
                    unset($bets[$k2]['options']);
                    if( $v2['source'] == 'pinnacle' ){ //筛选特定来源的赔率
                        $this->bet[0]['source'] = 'pinnacle';
                        $this->bet[0]['name'] = '平博';
                        $this->bet[0]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[0]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == '雷竞技' ){
                        $this->bet[1]['source'] = '雷竞技';
                        $this->bet[1]['name'] = '雷竞技';
                        $this->bet[1]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[1]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == 'bet365' ){
                        $this->bet[2]['source'] = 'bet365';
                        $this->bet[2]['name'] = 'bet365';
                        $this->bet[2]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[2]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == 'anggame' ){
                        $this->bet[3]['source'] = 'anggame';
                        $this->bet[3]['name'] = '安博';
                        $this->bet[3]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[3]['team_b'] = $bets[$k2]['team_b'];
                    }
                }
            }
            $m_team = [ //双方队伍,需要统计的字段
                'side'      => null,
                'money'     => null,
                'is_win'    => null,
                'battle_id' => null,
                'kill_count'=> null,
                'death_count'=> null,
                'assist_count'=> null,
                'is_ten_kills'=> null,
                'is_five_kills'=> null,
                'is_first_blood'=> null,
                'is_first_tower'=> null,
                'big_dragon_count'=> null,
                'small_dragon_count'=> null,
                'is_first_big_dragon'=> null,
                'tower_success_count'=> null,
                'is_first_small_dragon'=> null,
            ];
            $lists[$k]['team_a'] = array_merge($team_a,$m_team);
            $lists[$k]['team_b'] = array_merge($team_b,$m_team);
            $lists[$k]['bet'] = $this->bet;
            if( $lists[$k]['battle_ids'] ){
                $i = count($lists[$k]['battle_ids'])-1;
                $battle = $db->where('battle_id',$lists[$k]['battle_ids'][$i])->getOne('siam_lollive_battle','duration,team_stats');
                if( $battle ){
                    $battle['team_stats'] = json_decode($battle['team_stats'],true);
                    foreach ($battle['team_stats'] as $k1=>$v1){ //筛选比赛中双方队伍的数据统计
                        unset($v1['ban_ids']);
                        unset($v1['ban_list']);
                        unset($v1['pick_ids']);
                        unset($v1['pick_list']);
                        if( $v1['team_id'] == $v['team_a_id'] ){
                            $lists[$k]['team_a'] = $v1;
                        }else{
                            $lists[$k]['team_b'] = $v1;
                        }
                    }
                    $lists[$k]['team_a'] = array_merge($team_a,$lists[$k]['team_a']);
                    $lists[$k]['team_b'] = array_merge($team_b,$lists[$k]['team_b']);
                    $lists[$k]['duration'] = floor($battle['duration']/60).'′'.$battle['duration']%60;//游戏比赛时长
                }
            }
            $lists[$k]['live_video'] = $db->where('game','lol')//直播链接
            ->where('match_id',$v['match_id'])->get('siam_live_video',null,'name,url');
            $lists[$k]['league'] = $db->where('league_id',$v['league_id'])->getOne('siam_lolleague','name,name_en,short_name,logo');//联赛信息
        }
        if( isset($params['start_date']) && isset($params['end_date']) ){
            $start_time = strtotime($params['start_date'])*1000;
            $end_time = strtotime($params['end_date'])*1000;
            $db->where('start_time',$start_time,'>=')->where('start_time',$end_time,'<');
        }
        $count = $db->whereIn('status',$status)->count('siam_lolmatchs');
        return ['total'=>$count,'lists'=>$lists];
    }

    /**
     * dota比赛列表
     * @param $params
     * @return array
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \EasySwoole\Mysqli\Exceptions\WhereParserFail
     * @throws \Throwable
     */
    public function dotaMatchLists($params)
    {
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 20;
        $sort =  $params['sort'] ?? 'asc';
        $db = Mysql::defer('mysql');
        $status = isset($params['status']) ? [$params['status']] : [0,1,2];
        if( isset($params['start_date']) && isset($params['end_date']) ){
            $start_time = strtotime($params['start_date'])*1000;
            $end_time = strtotime($params['end_date'])*1000;
            $db->where('start_time',$start_time,'>=')->where('start_time',$end_time,'<');
        }

        $lists = $db->whereIn('status',$status)
            ->orderBy('start_time',$sort)
            ->get('siam_dotamatchs',[$pageSize * ($page  - 1), $pageSize]);//比赛列表
        foreach ($lists as $k=>$v){
            $lists[$k]['start_time'] = $this->getMsecToMescdate($v['start_time']);
            $lists[$k]['battle_ids'] = json_decode($v['battle_ids'],true);

            $team_b = $db->where('team_id',$v['team_b_id'])->getOne('siam_dotateams','name,short_name,logo') ? : [];
            $team_a = $db->where('team_id',$v['team_a_id'])->getOne('siam_dotateams','name,short_name,logo') ? : [];
            $lists[$k]['team_a'] = $team_a;
            $lists[$k]['team_b'] = $team_b;
            $bets = $db->where('match_id',$v['match_id'])->where('bet_type',1)->get('siam_dotabet',null,'options,source');
            //赔率
            if( $bets ){
                foreach ( $bets as $k2=>$v2 ){
                    $options = json_decode($v2['options'],true);
                    if( $options[0]['team_id'] == $v['team_b_id'] ){
                        $bets[$k2]['team_b'] = $options[0];
                    }else{
                        $bets[$k2]['team_a'] = $options[0];
                    }
                    if( $options[1]['team_id'] == $v['team_b_id'] ){
                        $bets[$k2]['team_b'] = $options[1];
                    }else{
                        $bets[$k2]['team_a'] = $options[1];
                    }
                    if( $v2['source'] == 'pinnacle' ){
                        $this->bet[0]['source'] = 'pinnacle';
                        $this->bet[0]['name'] = '=平博';
                        $this->bet[0]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[0]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == '雷竞技' ){
                        $this->bet[1]['source'] = '雷竞技';
                        $this->bet[1]['name'] = '雷竞技';
                        $this->bet[1]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[1]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == 'bet365' ){
                        $this->bet[2]['source'] = 'bet365';
                        $this->bet[2]['name'] = 'bet365';
                        $this->bet[2]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[2]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == 'anggame' ){
                        $this->bet[3]['source'] = 'anggame';
                        $this->bet[3]['name'] = '安博';
                        $this->bet[3]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[3]['team_b'] = $bets[$k2]['team_b'];
                    }
                    unset($bets[$k2]['options']);
                }
            }

            $lists[$k]['bet'] = $this->bet;

            $m_team = [
                'camp'  => null,
                'score' => null,
                'is_win'=> null,
                'battle_id' => null,
                'kill_count'=> null,
                'is_ten_kills'  => null,
                'is_first_blood'=> null,
                'money' => null
            ];
            $lists[$k]['team_a'] = array_merge($m_team,$team_a);
            $lists[$k]['team_b'] = array_merge($m_team,$team_b);

            if( $lists[$k]['battle_ids'] ){ //比赛列表展示最后一局战役
                $i = count($lists[$k]['battle_ids'])-1;
                $battle = $db->where('battle_id',$lists[$k]['battle_ids'][$i])->getOne('siam_dotalive_battle','id,duration,team_stats,player_stats');
                if( $battle ){
                    $battle['team_stats'] = json_decode($battle['team_stats'],true);
                    $battle['player_stats'] = json_decode($battle['player_stats'],true);
                    foreach ($battle['team_stats'] as $k1=>$v1){
                        unset($v1['team']);
                        unset($v1['ban_ids']);
                        unset($v1['ban_list']);
                        unset($v1['pick_ids']);
                        unset($v1['pick_list']);
                        if( $v1['team_id'] == $v['team_a_id'] ){
                            $lists[$k]['team_a'] = $v1;
                        }else{
                            $lists[$k]['team_b'] = $v1;
                        }
                    }
                    if( $battle['player_stats'] ){ //统计金钱
                        $a_money = 0;
                        $b_money = 0;
                        foreach ($battle['player_stats'] as $k2=>$v2){
                            if( $v2['team_id'] == $v['team_a_id'] ){
                                $a_money += $v2['gold'];
                            }
                            if( $v2['team_id'] == $v['team_b_id'] ){
                                $b_money += $v2['gold'];
                            }
                        }
                        $lists[$k]['team_a']['money'] = $a_money;
                        $lists[$k]['team_b']['money'] = $b_money;
                    }

                    $lists[$k]['team_a'] = array_merge($lists[$k]['team_a'],$team_a);
                    $lists[$k]['team_b'] = array_merge($lists[$k]['team_b'],$team_b);
                    $lists[$k]['duration'] = floor($battle['duration']/60).'′'.$battle['duration']%60;
                }
            }
            $lists[$k]['live_video'] = $db->where('game','dota') //直播源
            ->where('match_id',$v['match_id'])->get('siam_live_video',null,'name,url');
            //联赛
            $lists[$k]['league'] = $db->where('league_id',$v['league_id'])->getOne('siam_dotaleague','name,name_en,short_name,logo');
        }
        if( isset($params['start_date']) && isset($params['end_date']) ){
            $start_time = strtotime($params['start_date'])*1000;
            $end_time = strtotime($params['end_date'])*1000;
            $db->where('start_time',$start_time,'>=')->where('start_time',$end_time,'<');
        }
        $count = $db->whereIn('status',$status)->count('siam_dotamatchs');
        return ['total'=>$count,'lists'=>$lists];
    }

    /**
     * csgo比赛列表
     * @param $params
     * @return array
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \EasySwoole\Mysqli\Exceptions\WhereParserFail
     * @throws \Throwable
     */
    public function csgoMatchLists($params)
    {
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 20;
        $sort =  $params['sort'] ?? 'asc';
        $db = Mysql::defer('mysql');
        $status = isset($params['status']) ? [$params['status']] : [0,1,2];
        if( isset($params['start_date']) && isset($params['end_date']) ){
            $start_time = strtotime($params['start_date'])*1000;
            $end_time = strtotime($params['end_date'])*1000;
            $db->where('start_time',$start_time,'>=')->where('start_time',$end_time,'<');
        }
        $lists = $db->whereIn('status',$status)
            ->orderBy('start_time',$sort)
            ->get('siam_csgomatchs',[$pageSize * ($page  - 1), $pageSize]);//比赛列表
        foreach ( $lists as $k => $v ){
            $battle_list = json_decode($v['battle_list'],true);
            $map_names = $v['map_names'] ? json_decode($v['map_names'],true) : [];
            if( $map_names ){
                foreach ($map_names as $k2=>$v2){
                    if( !strstr($v2,'de_') ){
                        $map_names[$k2] = strtolower('de_'.$v2);
                    }
                }
            }
            $lists[$k]['map_names'] = $map_names;
            $lists[$k]['battle_list'] = $battle_list;
            $lists[$k]['map_bp'] = json_decode($v['map_bp'],true);
            $lists[$k]['start_time'] = $this->getMsecToMescdate($v['start_time']);
            $lists[$k]['team_a'] = $db->where('team_id',$v['team_a_id'])->getOne('siam_csgoteams','team_id,name,logo,country');
            $lists[$k]['team_b'] = $db->where('team_id',$v['team_b_id'])->getOne('siam_csgoteams','team_id,name,logo,country');
            $lists[$k]['team_a']['score'] = $v['team_a_score'];
            $lists[$k]['team_b']['score'] = $v['team_b_score'];
            $bets = $db->where('match_id',$v['match_id'])->where('bet_type',1)->get('siam_csgobet',null,'options,source');

            //赔率
            if( $bets ){
                foreach ( $bets as $k2=>$v2 ){
                    $options = json_decode($v2['options'],true);
                    if( $options[0]['team_id'] == $v['team_b_id'] ){
                        $bets[$k2]['team_b'] = $options[0];
                    }else{
                        $bets[$k2]['team_a'] = $options[0];
                    }
                    if( $options[1]['team_id'] == $v['team_b_id'] ){
                        $bets[$k2]['team_b'] = $options[1];
                    }else{
                        $bets[$k2]['team_a'] = $options[1];
                    }
                    unset($bets[$k2]['options']);
                    if( $v2['source'] == 'pinnacle' ){
                        $this->bet[0]['source'] = 'pinnacle';
                        $this->bet[0]['name'] = '平博';
                        $this->bet[0]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[0]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == '雷竞技' ){
                        $this->bet[1]['source'] = '雷竞技';
                        $this->bet[1]['name'] = '雷竞技';
                        $this->bet[1]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[1]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == 'bet365' ){
                        $this->bet[2]['source'] = 'bet365';
                        $this->bet[2]['name'] = 'bet365';
                        $this->bet[2]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[2]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == 'anggame' ){
                        $this->bet[3]['source'] = 'anggame';
                        $this->bet[3]['name'] = '安博';
                        $this->bet[3]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[3]['team_b'] = $bets[$k2]['team_b'];
                    }
                }
            }
            $fitst_win = '';//第一回合胜利
            $sixteen_win = '';//第16回合胜利
            $first_fivewin_round = '';//先胜利5回合
            $lists[$k]['bet'] = $this->bet;
            if( $map_names ){
                $i = count($map_names)-1;
                $m_n = $map_names[$i];
                if( !strstr($map_names[$i],'de_') ){
                    $m_n = strtolower('de_'.$m_n);
                }
                $battle = $db->where('match_id',$v['match_id'])->where('map_name',$m_n)->getOne('siam_csgolive_battle');
                $terrorist_players = json_decode($battle['terrorist_players'],true);
                $counter_terrorist_players = json_decode($battle['counter_terrorist_players'],true);
                $t_round_history_first_half = json_decode($battle['terrorist_round_history_first_half'],true);//t(匪)方上半场
                $t_round_history_second_half = json_decode($battle['terrorist_round_history_second_half'],true);//t(匪)方下半场

                $ct_round_history_first_half = json_decode($battle['counter_terrorist_round_history_first_half'],true) ? : [];//ct(警)方上半场
                $ct_round_history_second_half = json_decode($battle['counter_terrorist_round_history_second_half'],true) ? : [];//ct(警)方上半场

                if( $ct_round_history_first_half ){
                    $ct_all_round = array_merge_recursive($ct_round_history_first_half,$ct_round_history_second_half);//ct 全场
                }
                if( $t_round_history_first_half ){
                    $t_all_round = array_merge_recursive($t_round_history_first_half,$t_round_history_second_half);//t 全场
                }

                $t_kill_count = 0;//杀人书
                $t_death_count = 0;//死亡数
                $t_assist_count = 0;//助攻数
                $t_money = 0;//金钱数
                $ct_kill_count = 0;//杀人书
                $ct_death_count = 0;//死亡数
                $ct_assist_count = 0;//助攻数
                $ct_money = 0;//金钱数
                $t_first_half_win_nums = 0; //t方上半场胜利次数
                $t_second_half_win_nums = 0; //t方下半场胜利次数
                $ct_first_half_win_nums = 0;  //ct方上半场胜利次数
                $ct_second_half_win_nums = 0; //ct方下半场胜利次数


                if( $terrorist_players ){//队员
                    foreach ( $terrorist_players as $player ){
                        $t_kill_count += $player['kill_count'];
                        $t_death_count += $player['death_count'];
                        $t_assist_count += $player['assist_count'];
                        $t_money += $player['money'];
                    }
                }
                if( $counter_terrorist_players ){
                    foreach ($counter_terrorist_players as $player){
                        $ct_kill_count += $player['kill_count'];
                        $ct_death_count += $player['death_count'];
                        $ct_assist_count += $player['assist_count'];
                        $ct_money += $player['money'];
                    }
                }
                //上半场 t方
                if( $t_round_history_first_half ){ //匪
                    foreach ( $t_round_history_first_half as $tfh ){
                        if( $tfh['type'] != 'lost' ){
                            $t_first_half_win_nums++;
                        }
                    }
                }
                //下半场 t方
                if( $t_round_history_second_half ){
                    foreach ( $t_round_history_second_half as $tsh ){
                        if( $tsh['type'] != 'lost' ){
                            $t_second_half_win_nums++;
                        }
                    }
                }
                //上半场 ct方
                if( $ct_round_history_first_half ){ //警
                    foreach ( $ct_round_history_first_half as $ctfh ){
                        if( $ctfh['type'] != 'lost' ){
                            $ct_first_half_win_nums++;
                        }
                    }
                }
                //下半场 ct方
                if( $ct_round_history_second_half ){
                    foreach ( $ct_round_history_second_half as $ctsh ){
                        if( $ctsh['type'] != 'lost' ){
                            $ct_second_half_win_nums++;
                        }
                    }
                }

                if( isset($ct_all_round) && isset($t_all_round) ){
                    if( $ct_all_round[0]['type'] != 'lost' ){
                        $fitst_win = 'CT';//ct第一回合胜利
                    }else{
                        $fitst_win = 'T';//t第一回合胜利
                    }
                    if( isset($ct_all_round[15]) ){
                        if( $ct_all_round[15]['type'] != 'lost' ){
                            $sixteen_win = 'CT';//ct第一回合胜利
                        }else{
                            $sixteen_win = 'T';//t第一回合胜利
                        }
                    }
                    $ct_win = 0;
                    foreach ( $ct_all_round as $v1 ){ //ct上半场筛选达到5回合胜利，所需要的回合总数
                        if( $v1['type'] != 'lost' ){
                            $ct_win++;
                        }
                        if( $ct_win >= 5 ){
                            $ct_five_win = $v1['round'];
                            break;
                        }else{
                            $ct_five_win = $v1['round'];
                        }
                    }
                    $t_win = 0;
                    foreach ( $t_all_round as $v2 ){
                        if( $v2['type'] != 'lost' ){
                            $t_win++;
                        }
                        if( $t_win >= 5 ){
                            $t_five_win = $v2['round'];
                            break;
                        }else{
                            $t_five_win = $v2['round'];
                        }
                    }
                    if( $t_five_win < $ct_five_win ){
                        $first_fivewin_round = 'T'; //T先胜5回合
                    }else{
                        $first_fivewin_round = 'CT';//CT先胜5回合
                    }
                }


                if( $battle['terrorist_team_id'] == $v['team_a_id'] ){ //team_a 是匪
                    $lists[$k]['team_a']['is_bomb_planted'] = $battle['is_bomb_planted'];//是否安装炸弹
                    $lists[$k]['team_a']['kill_count'] = $t_kill_count;
                    $lists[$k]['team_a']['death_count'] = $t_death_count;
                    $lists[$k]['team_a']['assist_count'] = $t_assist_count;
                    $lists[$k]['team_a']['money']   = $t_money;
                    $lists[$k]['team_a']['role']   = 'T';
                    $lists[$k]['team_a']['first_half_win_nums'] = $t_first_half_win_nums;//t方上半场
                    $lists[$k]['team_a']['second_half_win_nums'] = $t_second_half_win_nums;//t方下半场

                    $lists[$k]['team_b']['kill_count'] = $ct_kill_count;
                    $lists[$k]['team_b']['death_count'] = $ct_death_count;
                    $lists[$k]['team_b']['assist_count'] = $ct_assist_count;
                    $lists[$k]['team_b']['money']   = $ct_money;
                    $lists[$k]['team_b']['role']   = 'CT';
                    $lists[$k]['team_b']['first_half_win_nums'] = $ct_first_half_win_nums;//t方上半场
                    $lists[$k]['team_b']['second_half_win_nums'] = $ct_second_half_win_nums;//t方下半场

                }else{ //team_a 是警
                    $lists[$k]['team_b']['is_bomb_planted'] = $battle['is_bomb_planted'];//是否安装炸弹
                    $lists[$k]['team_a']['kill_count'] = $ct_kill_count;
                    $lists[$k]['team_a']['death_count'] = $ct_death_count;
                    $lists[$k]['team_a']['assist_count'] = $ct_assist_count;
                    $lists[$k]['team_a']['money']   = $ct_money;
                    $lists[$k]['team_a']['role']   = 'CT';
                    $lists[$k]['team_a']['first_half_win_nums'] = $ct_first_half_win_nums;//t方上半场
                    $lists[$k]['team_a']['second_half_win_nums'] = $ct_second_half_win_nums;//t方下半场

                    $lists[$k]['team_b']['kill_count'] = $t_kill_count;
                    $lists[$k]['team_b']['death_count'] = $t_death_count;
                    $lists[$k]['team_b']['assist_count'] = $t_assist_count;
                    $lists[$k]['team_b']['money']   = $t_money;
                    $lists[$k]['team_b']['role']   = 'T';
                    $lists[$k]['team_b']['first_half_win_nums'] = $t_first_half_win_nums;//t方上半场
                    $lists[$k]['team_b']['second_half_win_nums'] = $t_second_half_win_nums;//t方下半场
                }
            }
            $lists[$k]['fitst_win'] = $fitst_win;
            $lists[$k]['sixteen_win'] = $sixteen_win;
            $lists[$k]['first_fivewin_round'] = $first_fivewin_round;
            $lists[$k]['league'] = $db->where('league_id',$v['league_id'])->getOne('siam_csgoleague','league_id,logo,name');
            unset($lists[$k]['battle_list']);
            $lists[$k]['battle_ids'] = json_decode($v['battle_ids'],true);
            if( $v['bo'] == 1 ){
                if( $v['team_a_score'] > $v['team_b_score'] ){
                    $lists[$k]['team_a_score'] = 1;
                    $lists[$k]['team_b_score'] = 0;
                }else{
                    $lists[$k]['team_b_score'] = 1;
                    $lists[$k]['team_a_score'] = 0;
                }
            }
        }
        if( isset($params['start_date']) && isset($params['end_date']) ){
            $start_time = strtotime($params['start_date'])*1000;
            $end_time = strtotime($params['end_date'])*1000;
            $db->where('start_time',$start_time,'>=')->where('start_time',$end_time,'<');
        }
        $count = $db->whereIn('status',$status)->count('siam_csgomatchs');
        return ['total'=>$count,'lists'=>$lists];
    }

    /**
     * 王者荣耀比赛列表
     * @param $params
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \EasySwoole\Mysqli\Exceptions\WhereParserFail
     * @throws \Throwable
     */
    public function kogMatchLists($params)
    {
        $db = Mysql::defer('mysql');
        $status = isset($params['status']) ? [$params['status']] : [0,1,2];
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $sort =  $params['sort'] ?? 'asc';

        if( isset($params['start_date']) && isset($params['end_date']) ){
            $start_time = strtotime($params['start_date'])*1000;
            $end_time = strtotime($params['end_date'])*1000;
            $db = $db->whereBetween('start_time',[$start_time,$end_time]);
        }

        $lists = $db->whereIn('status',$status)
            ->orderBy('start_time',$sort)
            ->get('siam_kogmatchs',[$pageSize * ($page  - 1), $pageSize]); //比赛列表
        foreach ($lists as $k=>$v){
            $lists[$k]['start_time'] = $this->getMsecToMescdate($v['start_time']);
            $lists[$k]['match_time'] = $v['start_time'];
            $lists[$k]['battle_ids'] = json_decode($v['battle_ids'],true);
            $team_b = $db->where('team_id',$v['team_b_id'])->getOne('siam_kogteams','name,name_en,short_name,logo') ? : [];//队伍a详情
            $team_a = $db->where('team_id',$v['team_a_id'])->getOne('siam_kogteams','name,name_en,short_name,logo') ? : [];;//队伍b详情
            $bets = $db->where('match_id',$v['match_id'])->where('bet_type',1)->get('siam_kogbet',null,'options,source');
            $lists[$k]['team_a'] = $team_a;
            $lists[$k]['team_b'] = $team_b;

            //赔率
            if( $bets ){
                foreach ( $bets as $k2=>$v2 ){
                    $options = json_decode($v2['options'],true);
                    if( $options[0]['team_id'] == $v['team_b_id'] ){
                        $bets[$k2]['team_b'] = $options[0];
                    }else{
                        $bets[$k2]['team_a'] = $options[0];
                    }
                    if( $options[1]['team_id'] == $v['team_b_id'] ){
                        $bets[$k2]['team_b'] = $options[1];
                    }else{
                        $bets[$k2]['team_a'] = $options[1];
                    }
                    unset($bets[$k2]['options']);
                    if( $v2['source'] == 'pinnacle' ){
                        $this->bet[0]['source'] = 'pinnacle';
                        $this->bet[0]['name'] = '平博';
                        $this->bet[0]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[0]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == '雷竞技' ){
                        $this->bet[1]['source'] = '雷竞技';
                        $this->bet[1]['=name'] = '雷竞技';
                        $this->bet[1]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[1]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == 'bet365' ){
                        $this->bet[2]['source'] = 'bet365';
                        $this->bet[2]['name'] = 'bet365';
                        $this->bet[2]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[2]['team_b'] = $bets[$k2]['team_b'];
                    }
                    if( $v2['source'] == 'anggame' ){
                        $this->bet[3]['source'] = 'anggame';
                        $this->bet[3]['name'] = '安博';
                        $this->bet[3]['team_a'] = $bets[$k2]['team_a'];
                        $this->bet[3]['team_b'] = $bets[$k2]['team_b'];
                    }
                }
            }
            $m_team = [
                'side'      => null,
                'money'     => null,//经济
                'is_win'    => null,
                'battle_id' => null,
                'kill_count'=> null,
                'death_count'=> null,
                'assist_count'=> null,
                'big_dragon_count'=> null,//主宰数
                'small_dragon_count'=> null,//暴君数
                'tower_success_count'=> null, //拆塔
            ];
            $lists[$k]['team_a'] = array_merge($team_a,$m_team);
            $lists[$k]['team_b'] = array_merge($team_b,$m_team);
            $lists[$k]['bet'] = $this->bet;
            if( $lists[$k]['battle_ids'] ){
                $i = count($lists[$k]['battle_ids'])-1;
                $battle = $db->where('battle_id',$lists[$k]['battle_ids'][$i])->getOne('siam_koglive_battle','duration,team_stats');
                if( $battle ){
                    $battle['team_stats'] = json_decode($battle['team_stats'],true);
                    foreach ($battle['team_stats'] as $k1=>$v1){
                        unset($v1['ban_ids']);
                        unset($v1['ban_list']);
                        unset($v1['pick_ids']);
                        unset($v1['pick_list']);
                        if( $v1['team_id'] == $v['team_a_id'] ){
                            $lists[$k]['team_a'] = $v1;
                        }else{
                            $lists[$k]['team_b'] = $v1;
                        }
                    }
                    $lists[$k]['team_a'] = array_merge($team_a,$lists[$k]['team_a']);
                    $lists[$k]['team_b'] = array_merge($team_b,$lists[$k]['team_b']);
                    $lists[$k]['duration'] = floor($battle['duration']/60).'′'.$battle['duration']%60;
                }
            }
            $lists[$k]['live_video'] = [];
            $lists[$k]['league'] = $db->where('league_id',$v['league_id'])->getOne('siam_kogleague','name,short_name,logo');//联赛信息
        }
        if( isset($params['start_date']) && isset($params['end_date']) ){
            $start_time = strtotime($params['start_date'])*1000;
            $end_time = strtotime($params['end_date'])*1000;
            $db->where('start_time',$start_time,'>=')->where('start_time',$end_time,'<');
        }
        $count = $db->whereIn('status',$status)->count('siam_kogmatchs');
        return ['total'=>$count,'lists'=>$lists];
    }

    //毫秒
    public function getMsecToMescdate($msectime)
    {
        $msectime = $msectime * 0.001;
        if(strstr($msectime,'.')){
            sprintf("%01.3f",$msectime);
            list($usec, $sec) = explode(".",$msectime);
            $sec = str_pad($sec,3,"0",STR_PAD_RIGHT);
        }else{
            $usec = $msectime;
            $sec = "000";
        }
        $date = date("Y-m-d H:i:s",$usec);
        return $mescdate = str_replace('x', $sec, $date);
    }
}